package xgenerator.generators.jgenerator;

import xgenerator.generators.WebMVC;
import xgenerator.model.ModelMetadata;
import xgenerator.util.StringUtils;

/**
 * <p>
 * Title: MVC Java Web应用代码生成器
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2012
 * </p>
 * <p>
 * Company: liss
 * </p>
 * @author <a href="mailto:shushanlee@msn.com">liss</a>
 * @version 1.0
 * @see xgenerator.generators.jgenerator.JGenerator
 * @see xgenerator.generators.WebMVC
 * @since 1.0
 */
public abstract class JWebAppGenerator extends JGenerator implements WebMVC {
	
	/*
	 * web层代码目录
	 */
	public static String WEB_DIR = "web";
	
	/*
	 * web代码目录名
	 */
	public static String WEB_METAINF_DIR = "META-INF";
	public static String WEB_WEBINF_DIR = "WEB-INF";
	public static String WEB_TAGLIB_DIR = "tags";

	/*
	 * XML文件扩展名
	 */
	public static final String XML_EXT = ".xml";
	
	/**
	 * <p>
	 * Title：生成代码，java代码按子系统层划分，页面按模块划分
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param modelMetadata
	 * @return
	 * @see xgenerator.generators.Generator#generateCode(xgenerator.model.ModelMetadata)
	 */
	public String generateCode(ModelMetadata modelMetadata) {
		StringBuffer codes = new StringBuffer();
		
		/*
		 * 生成视图代码
		 */
		if(modelMetadata.isGenerateViewCode()) {
			String viewCode = generateViewCode(modelMetadata);
			if(StringUtils.isNotBlank(viewCode)) {
				codes.append(viewCode);
			}
		}
		/*
		 * 生成控制器代码
		 */
		if(modelMetadata.isGenerateControllerCode()) {
			String controllerCode = generateControllerCode(modelMetadata);
			if(StringUtils.isNotBlank(controllerCode)) {
				codes.append(controllerCode);
			}
		}
		/*
		 * 生成业务层代码
		 */
		if(modelMetadata.isGenerateAppCode()) {
			String appCode = generateAppCode(modelMetadata);
			if(StringUtils.isNotBlank(appCode)) {
				codes.append(appCode);
			}
		}
		/*
		 * 生成持久层代码
		 */
		if(modelMetadata.isGeneratePersistenceCode()) {
			String persistenceCode = generatePersistenceCode(modelMetadata);
			if(StringUtils.isNotBlank(persistenceCode)) {
				codes.append(persistenceCode);
			}
		}
		
		return codes.toString();
	}
	
}
